package com.woniuxy.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import com.woniuxy.DTO.AddAnimalTypeDTO;
import com.woniuxy.DTO.AlgsAnimalDTO;
import com.woniuxy.domain.AlgsAnimalType;
import com.woniuxy.domain.AlgsCage;
import com.woniuxy.mapper.AlgsAnimalTypeMapper;
import com.woniuxy.mapper.AlgsCageMapper;
import com.woniuxy.vo.AlgsAnimalNameVO;
import com.woniuxy.vo.AlgsCageCountByAnimalNameVO;
import com.woniuxy.vo.AlgsSelectAnimalNameByAnimalIDVO;
import jdk.internal.org.objectweb.asm.TypeReference;
import net.sf.jsqlparser.expression.TryCastExpression;
import org.springframework.stereotype.Service;
import com.woniuxy.mapper.AlgsAnimalMapper;
import com.woniuxy.domain.AlgsAnimal;
import com.woniuxy.service.IAlgsAnimalService;

import javax.annotation.Resource;

/**
 * 动物库存管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-03-20
 */
@Service
public class AlgsAnimalServiceImpl implements IAlgsAnimalService 
{
    @Resource
    private AlgsAnimalMapper algsAnimalMapper;
    @Resource
    private AlgsCageMapper algsCageMapper;
    @Resource
    private AlgsAnimalTypeMapper algsAnimalTypeMapper;
    /**
     * 查询动物库存管理
     * 
     * @param animalId 动物库存管理主键
     * @return 动物库存管理
     */
    @Override
    public AlgsAnimal selectAlgsAnimalByAnimalId(Long animalId)
    {
        return algsAnimalMapper.selectAlgsAnimalByAnimalId(animalId);
    }

    /**
     * 查询动物库存管理列表
     * 
     * @param algsAnimal 动物库存管理
     * @return 动物库存管理
     */
    @Override
    public List<AlgsAnimal> selectAlgsAnimalList(AlgsAnimal algsAnimal)
    {
        return algsAnimalMapper.selectAlgsAnimalList(algsAnimal);
    }

    /**
     * 新增动物库存管理
     * 
     * @param algsAnimal 动物库存管理
     * @return 结果
     */
    @Override
    public int insertAlgsAnimal(AlgsAnimal algsAnimal)
    {
        return algsAnimalMapper.insertAlgsAnimal(algsAnimal);
    }

    /**
     * 动物出库管理，进行事务操作
     * 
     * @param algsAnimal 动物库存管理
     * @return 结果
     */
    @Override
    public int updateAlgsAnimal(AlgsAnimal algsAnimal)
    {
        AlgsAnimal algsAnimal1 = algsAnimalMapper.selectAlgsAnimalByAnimalId(algsAnimal.getAnimalId());
        if(algsAnimal1.getAnimalNumber()>=1) {

            algsAnimal1.setAnimalNumber(algsAnimal1.getAnimalNumber() -1);
            List<AlgsSelectAnimalNameByAnimalIDVO> list = algsAnimalMapper.AnimalDeliveryRecord(algsAnimal.getAnimalId(), algsAnimal.getCageSizeId());

           int i = 0;
            while (list.get(i).getCageBy1()!=null && list.get(i).getCageBy1().length()!=0){
                i++;
                if (i>=list.size()-1){
                    break;
                }
            }
            System.out.println(i);
            AlgsSelectAnimalNameByAnimalIDVO algsSelectAnimalNameByAnimalIDVO = list.get(i);

            Long cageSizeId = algsSelectAnimalNameByAnimalIDVO.getCageId();
            AlgsCage algsCage = algsCageMapper.selectByPrimaryKey(cageSizeId);
            if (algsCage.getCageSizeId()==1){
                algsCage.setCageBy1("");
                algsCage.setCageTypeId(72l);
                algsCage.setCageBy3("");
                algsCageMapper.updateByPrimaryKeySelective(algsCage);
            }else if (algsCage.getCageSizeId()==2){
                algsCage.setCageBy1("");
                algsCage.setCageTypeId(73l);
                algsCage.setCageBy3("");
                algsCageMapper.updateByPrimaryKeySelective(algsCage);
            }else if (algsCage.getCageSizeId()==3){
                algsCage.setCageBy1("");
                algsCage.setCageTypeId(76l);
                algsCage.setCageBy3("");
                algsCageMapper.updateByPrimaryKeySelective(algsCage);
            }

            return algsAnimalMapper.updateAlgsAnimal(algsAnimal1);
        }
        else {
            return 0;
        }
    }

    /**
     * 批量删除动物库存管理
     * 
     * @param animalIds 需要删除的动物库存管理主键
     * @return 结果
     */
    @Override
    public int deleteAlgsAnimalByAnimalIds(Long[] animalIds)
    {
        return algsAnimalMapper.deleteAlgsAnimalByAnimalIds(animalIds);
    }

    /**
     * 删除动物库存管理信息
     * 
     * @param animalId 动物库存管理主键
     * @return 结果
     */
    @Override
    public int deleteAlgsAnimalByAnimalId(Long animalId)
    {
        return algsAnimalMapper.deleteAlgsAnimalByAnimalId(animalId);
    }

    @Override
    public ArrayList<Object> selectAllAnimal(AlgsAnimalDTO algsAnimalDTO) {
      //  List<AlgsAnimalVO> algsAnimalList = algsAnimalMapper.selectAllAnimal(algsAnimalDTO);
        ArrayList<Object> algsAnimalVOS = new ArrayList<>();
        List<AlgsAnimalNameVO> algsAnimalNameList = algsAnimalMapper.selectAllAnimalName();
        for (AlgsAnimalNameVO algsAnimalNameVO : algsAnimalNameList) {
            AlgsAnimal algsAnimal = algsAnimalMapper.selectAnimalByAnimalName(algsAnimalNameVO.getAnimalName());
            AlgsCageCountByAnimalNameVO num = algsAnimalMapper.selectByAnimalNameByCageCount(algsAnimalNameVO.getAnimalName());
            algsAnimal.setAnimalNumber(num.getAnimalNum());
            algsAnimalMapper.updateAlgsAnimal(algsAnimal);
            algsAnimalVOS.add(algsAnimal);
        }
        return algsAnimalVOS;
    }

    @Override
    public List<AlgsSelectAnimalNameByAnimalIDVO> selectAnimalNameByAnimalID(Long animalId) {
        return algsAnimalMapper.selectAnimalNameByAnimalID(animalId);
    }

    @Override
    public List<AlgsSelectAnimalNameByAnimalIDVO> AnimalDeliveryRecord(Long animalId, Long cageSizeId) {
        return algsAnimalMapper.AnimalDeliveryRecord(animalId,cageSizeId);
    }

    @Override
    public List<AlgsAnimalNameVO> selectAllAnimalName() {
        return algsAnimalMapper.selectAllAnimalName();
    }

    @Override
    public AlgsAnimal selectAnimalByAnimalName(String animalName) {
        return algsAnimalMapper.selectAnimalByAnimalName(animalName);
    }

    @Override
    public AlgsCageCountByAnimalNameVO selectByAnimalNameByCageCount(String animalName) {
        return algsAnimalMapper.selectByAnimalNameByCageCount(animalName);
    }

    @Override
    public int addAnimalType(AddAnimalTypeDTO addAnimalTypeDTO) {
        try {
            List<AlgsCage> list = algsCageMapper.selectByCageSizeIdAndTypeId(Long.parseLong(addAnimalTypeDTO.getCageSizeId()));
            if(list!=null){
                AlgsAnimalType algsAnimalType = new AlgsAnimalType();
                algsAnimalType.setAnimalMsg(addAnimalTypeDTO.getAnimalMsg());
                algsAnimalType.setCageBy1(addAnimalTypeDTO.getAnimalName());
                algsAnimalTypeMapper.insertSelective(algsAnimalType);
                AlgsCage algsCage = list.get(0);
                algsCage.setCageSizeId(Long.parseLong(addAnimalTypeDTO.getCageSizeId()));
                algsCage.setCageDate(new Date());
                algsCage.setCageBy1(addAnimalTypeDTO.getAnimalName());
                algsCage.setCageBy3(new Random().nextInt(10000)+"");
                    if(Integer.parseInt(addAnimalTypeDTO.getCageSizeId())==1){
                        algsCage.setCageTypeId(71l);
                    }else if(Integer.parseInt(addAnimalTypeDTO.getCageSizeId())==2){
                        algsCage.setCageTypeId(74l);
                    }else if(Integer.parseInt(addAnimalTypeDTO.getCageSizeId())==3) {
                        algsCage.setCageTypeId(75l);
                    }
                    algsCageMapper.updateByPrimaryKeySelective(algsCage);
                AlgsAnimal algsAnimal = new AlgsAnimal();
                algsAnimal.setAnimalName(addAnimalTypeDTO.getAnimalName());
                algsAnimal.setAnimalDate(new Date());
                algsAnimal.setCageSizeId(Long.parseLong(addAnimalTypeDTO.getCageSizeId()));
                algsAnimal.setAnimalBy2(addAnimalTypeDTO.getAnimalBy2());
                algsAnimalMapper.insertAlgsAnimal(algsAnimal);
                return 1;
            }else {
                return 0;
            }
        }catch (Exception e){

        }
            return 3;

    }
}
